package com.woniu.defecttracking.controller.bugs;


import com.woniu.defecttracking.annotation.WorkLog;
import com.woniu.defecttracking.annotation.WorkLogAuto;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.commens.OssUtil;
import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.BugCountsDto;
import com.woniu.defecttracking.dto.BugInfoDto;
import com.woniu.defecttracking.dto.BugInfosDto;
import com.woniu.defecttracking.entity.BugInfo;
import com.woniu.defecttracking.entity.Project;
import com.woniu.defecttracking.exception.BugInfoException;
import com.woniu.defecttracking.param.*;
import com.woniu.defecttracking.service.BugInfoService;
import com.woniu.defecttracking.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author tiangle
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/bugInfo")
public class BugInfoController {
    @Autowired
    private BugInfoService bugInfoService;
    @Autowired
    private OssUtil oc;
    @Autowired
    private ProjectService projectService;
    /**
    * @作者: tiangle
    * @描述：新增一条buginfo
    * @时间：2022/1/2 16:27
    * @参数类型:[addAndEditBugInfo]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("addNew")
    public Result saveBugInfo(@RequestBody AddAndEditBugInfo addAndEditBugInfo) throws BugInfoException {
        bugInfoService.addOne(addAndEditBugInfo);
        return Result.getInstance(null).setMessage("添加成功！");
    }

    /**
    * @作者: tiangle
    * @描述：修改一条bugInfo
    * @时间：2022/1/2 16:40
    * @参数类型:[addAndEditBugInfo]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
//    @WorkLogAuto(logDesc = "bugDesc" ,workHours = "actWorkHours")
    @PostMapping("editOne")
    public Result editBugInfo(@RequestBody AddAndEditBugInfo addAndEditBugInfo) throws BugInfoException {
        bugInfoService.editOne(addAndEditBugInfo);
        return Result.getInstance(null).setMessage("修改成功！");
    }


    /**
    * @作者: tiangle
    * @描述：删除一条bugInfo
    * @时间：2022/1/2 16:41
    * @参数类型:[id]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("deleteOne/{id}")
    public Result deleteBugInfo(@PathVariable Integer id) throws BugInfoException {
        boolean b = bugInfoService.deleteOne(id);
        if (b){
            return Result.getInstance(null).setMessage("删除成功！");
        }

        throw new BugInfoException("删除失败",433);
    }

    /**
    * @作者: tiangle
    * @描述：删除多条bugInfo
    * @时间：2022/1/2 16:52
    * @参数类型:[ids]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("deletes")
    public Result deleteBugInfos(@RequestBody IdList ids) throws BugInfoException {
        boolean b = bugInfoService.deletes(ids.getIds());

        if (b){
            return Result.getInstance(null).setMessage("删除成功！");
        }

        throw new BugInfoException("删除失败",433);
    }


    /**
    * @作者: tiangle
    * @描述：根据信息筛选查询所有并分页
    * @时间：2022/1/2 19:21
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("getAll")
    public Result getAll(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugInfosDto all = bugInfoService.getAll(allBugInfosParam);
        return Result.getInstance(all);
    }

    /**
    * @作者: tiangle
    * @描述：根据id查询bugInfo
    * @时间：2022/1/2 19:24
    * @参数类型:[id]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("getOne/{id}")
    public Result getOne(@PathVariable Integer id) throws BugInfoException {
        BugInfoDto byId = bugInfoService.getOne(id);
        if (byId==null){
            throw new BugInfoException("查询失败");
        }
        Project byId1 = projectService.getById(byId.getProjectId());
        byId.setProjectName(byId1.getProjectName());

        return Result.getInstance(byId);
    }
    /**
    * @作者: tiangle
    * @描述：返回新建状态的bugInfo
    * @时间：2022/1/3 13:41
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("getAllNew")
    public Result getAllNew(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugInfosDto all = bugInfoService.getAllNew(allBugInfosParam);
        return Result.getInstance(all);
    }


    /**
    * @作者: tiangle
    * @描述：返回进行状态的bugInfo
    * @时间：2022/1/3 13:43
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("getAllGo")
    public Result getAllGo(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugInfosDto all = bugInfoService.getAllGo(allBugInfosParam);
        return Result.getInstance(all);
    }
    /**
    * @作者: tiangle
    * @描述：返回进行和解决的bugInfo
    * @时间：2022/1/3 13:49
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("getAllGoAndEnd")
    public Result getAllGoAndEnd(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugInfosDto all = bugInfoService.getAllGoAndEnd(allBugInfosParam);
        return Result.getInstance(all);
    }

    /**
    * @作者: tiangle
    * @描述：分配给一个人
    * @时间：2022/1/3 13:58
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("giveOne")
    public Result giveOne(@RequestBody AddAndEditBugInfo addAndEditBugInfo) throws BugInfoException {
        boolean b = bugInfoService.giveOne(addAndEditBugInfo);
        if (b)
            return Result.getInstance(null).setMessage("分配成功");
        throw new BugInfoException("分配失败",1014);
    }

    /**
    * @作者: tiangle
    * @描述：根据登录id得到所有待解决的bugInfo（开发者查）
    * @时间：2022/1/3 14:07
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("getAllKo")
    public Result getAllKo(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugInfosDto all = bugInfoService.getAllKo(allBugInfosParam);
        return Result.getInstance(all);
    }
    /**
    * @作者: tiangle
    * @描述：开发者处理bug后提交使用，添加日志注解
    * @时间：2022/1/3 17:13
    * @参数类型:[koBugInfoParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("koBug")
    public Result koBug(@RequestBody KoBugInfoParam koBugInfoParam) throws BugInfoException {
        boolean b = bugInfoService.koBug(koBugInfoParam);

        if (b)
            return Result.getInstance(null);
        throw new BugInfoException("无法处理",1015);
    }
    /**
    * @作者: tiangle
    * @描述：反馈使用，此处添加反馈注解或者前端和反馈同时处理或者方法中调用添加反馈的接口
    * @时间：2022/1/3 17:22
    * @参数类型:[closeBugParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("closeBug")
    public Result closeBug(@RequestBody CloseBugParam closeBugParam) throws BugInfoException {
        bugInfoService.closeBug(closeBugParam);
        return Result.getInstance(null);
    }
    /**
    * @作者: tiangle
    * @描述：统计bugtype返回name、value的对象数组
    * @时间：2022/1/3 17:52
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("bugTypeCount")
    public Result bugTypeCount(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugCountsDto bugCountsDto = bugInfoService.bugTypeCount(allBugInfosParam);
        return Result.getInstance(bugCountsDto);
    }
    /**
    * @作者: tiangle
    * @描述：统计severity返回name、value的对象数组
    * @时间：2022/1/3 18:05
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("severityCount")
    public Result severityCount(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugCountsDto bugCountsDto = bugInfoService.savarityCount(allBugInfosParam);
        return Result.getInstance(bugCountsDto);
    }
    /**
    * @作者: tiangle
    * @描述：统计priority返回name、value的对象数组
    * @时间：2022/1/3 18:05
    * @参数类型:[allBugInfosParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("priorityCount")
    public Result priorityCount(@RequestBody AllBugInfosParam allBugInfosParam) throws BugInfoException {
        BugCountsDto bugCountsDto = bugInfoService.priorityCount(allBugInfosParam);
        return Result.getInstance(bugCountsDto);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        String name = file.getOriginalFilename();
        String fileName = System.currentTimeMillis() + name.substring(name.lastIndexOf("."));

        String url = oc.upload(fileName,  file.getInputStream());
        return Result.getInstance(url);
    }

    @PostMapping("/delete")
    public Result delete(String fileName){
        System.out.println(fileName);
        oc.deleteFile(fileName);
        return Result.getInstance(null);
    }



}

